package com.xie.smfs.entity;

import com.xie.smfs.item.RedGhostCandleItem;
import com.xie.smfs.registry.ModEffects;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.damage.DamageType;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.util.Identifier;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.RaycastContext;
import net.minecraft.world.World;

public class DeathSightGhostEntity extends GhostEntity {
    // B级恐怖，0级鬼域
    private static final boolean HAS_GHOST_DOMAIN = false;
    private static final int GHOST_DOMAIN_LEVEL = 0;
    private static final double GHOST_DOMAIN_RADIUS = 0.0;
    private static final char TERROR_LEVEL = 'B';

    public DeathSightGhostEntity(EntityType<? extends GhostEntity> entityType, World world) {
        super(entityType, world, HAS_GHOST_DOMAIN, GHOST_DOMAIN_LEVEL, GHOST_DOMAIN_RADIUS, TERROR_LEVEL);
        this.ghostLevel = 0; // 设置鬼等级
    }

    @Override
    protected boolean shouldAttackPlayer(PlayerEntity player) {
        // 基础条件：未被压制、未死机、玩家未持蜡烛
        if (isSuppressed() || isDeadlocked() || RedGhostCandleItem.isHoldingCandle(player)) {
            return false;
        }

        // 检查玩家是否在有效范围内（例如20格内）
        double distance = this.squaredDistanceTo(player);
        if (distance > 400.0) {
            return false;
        }

        // 检查玩家是否面向鬼（视线方向）
        Vec3d playerLookVec = player.getRotationVec(1.0F).normalize();
        Vec3d toGhostVec = new Vec3d(this.getX() - player.getX(),
                this.getEyeY() - player.getEyeY(),
                this.getZ() - player.getZ()).normalize();

        // 计算点积，如果大于0.5（大约60度内）则认为玩家面向鬼
        double dot = playerLookVec.dotProduct(toGhostVec);
        if (dot < 0.5) {
            return false;
        }

        // 可选：检查是否有障碍物阻挡（例如墙壁）
        // 如果不需要可以省略
        if (this.getWorld().raycast(new RaycastContext(player.getEyePos(), this.getEyePos(),
                RaycastContext.ShapeType.COLLIDER, RaycastContext.FluidHandling.NONE, this)).getType() != HitResult.Type.MISS) {
            return false;
        }

        return true;
    }

    @Override
    protected void executeAttack(PlayerEntity player) {
        // 立即造成致命伤害
        RegistryEntry<DamageType> ghostDamageType = this.getWorld().getRegistryManager().get(RegistryKeys.DAMAGE_TYPE)
                .getEntry(RegistryKey.of(RegistryKeys.DAMAGE_TYPE, new Identifier("smfs", "ghost")))
                .orElseThrow(() -> new IllegalStateException("Ghost damage type not found"));
        DamageSource damageSource = new DamageSource(ghostDamageType, this);
        player.damage(damageSource, 100.0F);
        // 施加恐怖效果
}

    @Override
    public void tick() {
        super.tick();
        // 每tick检查并攻击可见玩家
        if (!this.getWorld().isClient && !isDeadlocked() && !isSuppressed()) {
            this.getWorld().getPlayers().stream()
                    .filter(this::shouldAttackPlayer)
                    .forEach(this::executeAttack);
        }
    }
}